package myfunc;

import basis.DbConnect;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

public class myfind_air extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public myfind_air() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // TODO Auto-generated method stub
        Vector<String> up = new Vector();
        Vector<String> down = new Vector();
        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=utf-8");
            String value = request.getParameter("address");
            Connection con = DbConnect.getConnection();
            String sql = "select up,down from planeget where startaddress=?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, value);
            pstmt.executeQuery();
            ResultSet rst = pstmt.getResultSet();
            String s = "{'d':[";
            if (rst.next()) {
                String upplane = rst.getString(1);
                String downplane = rst.getString(2);
                String para1[];
                String para2[];
                if (upplane != null) {
                    para1 = upplane.split(",");
                    for (int i = 0; i < para1.length; i++) {
                        up.add(para1[i]);
                    }
                }
                if (downplane != null) {
                    para2 = downplane.split(",");
                    for (int i = 0; i < para2.length; i++) {
                        down.add(para2[i]);
                    }
                }
                System.out.println(up + "-----" + down);
                rst.close();
                pstmt.close();
                for (int i = 0; i < up.size(); i++) {
                    String key = up.get(i);
                    sql = "select *from planebasis where planeno=?";
                    PreparedStatement Realpstmt = con.prepareStatement(sql);
                    Realpstmt.setString(1, key);
                    Realpstmt.executeQuery();
                    ResultSet Realrst = Realpstmt.getResultSet();
                    if (Realrst.next()) {
                        s += "{'d2':[";
                        for (int j = 1; j <= 8; j++) {
                            s += "'" + Realrst.getString(j) + "',";
                        }
                        s += "]},";
                    }
                    Realrst.close();
                    Realpstmt.close();
                }
                for (int i = 0; i < down.size(); i++) {
                    String key = down.get(i);
                    sql = "select *from planebasis where planeno=?";
                    PreparedStatement Realpstmt = con.prepareStatement(sql);
                    Realpstmt.setString(1, key);
                    Realpstmt.executeQuery();
                    ResultSet Realrst = Realpstmt.getResultSet();
                    if (Realrst.next()) {
                        s += "{'d2':[";
                        for (int j = 1; j <= 8; j++) {
                            s += "'" + Realrst.getString(j) + "',";
                        }
                        s += "]},";
                    }
                    Realrst.close();
                    Realpstmt.close();
                }
            }
            s += "]}";
            System.out.println(s);
            PrintWriter out = response.getWriter();
            out.print(s);
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
